const Controller = require('egg').Controller;

class ClazzController extends Controller {
    // restfun index/create/destroy/update
    async index() {
        // let id = this.ctx.request.query.id;
        //findAll() : 括号中可以添加条件对象，where: 查询条件
        let clazzList = await this.app.model.Clazz.findAll({
            // where: {
            //     id: id
            // }
        });
        this.ctx.body = clazzList
    }
    async create() {
        let name = this.ctx.request.body.name;
        // create() ：添加数据
        await this.app.model.Clazz.create({
            name: name
        })
        this.ctx.body = name
        // this.ctx.body = "create"

    }
    async destroy() {
        // this.app.model.Clazz.destroy({where:{id:1}}); //通过条件删除数据

        let id = this.ctx.params.id;
        await this.app.model.Clazz.destroy({
            where:{
                id:id
            }
        })
        this.ctx.body = "删除成功"

    }
    async update() {
        //  this.app.model.Clazz.update({name: “xx”},{where:{id:1}}); //通过条件修改数据
        let id = this.ctx.params.id;
        let name =  this.ctx.request.body.name;
        await this.app.model.Clazz.update({
            name: name
        }, {
            where: {
                id: id
            }
        })

        this.ctx.body = "修改成功"

    }

}

module.exports = ClazzController;
